// pages/food/food.js
const app = getApp();
Page({
  data: {
    inputShowed: false,
    inputVal: "",
    show_select_line: false,
    functionList: [
      {
        "title":"火锅",
        "url":"https://z3.ax1x.com/2021/05/26/29eQBR.png",
      },
      {
        "title":"自助餐",
        "url":"https://z3.ax1x.com/2021/05/26/29elH1.png",
      },
      {
        "title":"蛋糕奶茶",
        "url":"https://z3.ax1x.com/2021/05/26/29eMu9.png",
      },
      {
        "title":"小吃快餐",
        "url":"https://z3.ax1x.com/2021/05/26/29eujJ.png",
      },
      {
        "title":"烧烤烤肉",
        "url":"https://z3.ax1x.com/2021/05/26/29e9Xj.png",
      },
      {
        "title":"川湘菜",
        "url":"https://z3.ax1x.com/2021/05/26/29epcQ.png",
      },
      {
        "title":"西餐",
        "url":"https://z3.ax1x.com/2021/05/26/29eS1g.png",
      },
      {
        "title":"日韩料理",
        "url":"https://z3.ax1x.com/2021/05/26/29Zz9S.png",
      },
      {
        "title":"海鲜",
        "url":"https://z3.ax1x.com/2021/05/26/29Zjtf.png",
      },
      {
        "title":"江浙菜",
        "url":"https://z3.ax1x.com/2021/05/26/29Zvh8.png",
      },
    ],
    storeList:[],
    storeList1: [{
      storeId: 1,
      storeName: '竹林香米线',
      storeImgUrl: 'https://z3.ax1x.com/2021/05/12/gw15o6.jpg',
      score: 4.4,
      saleMonth: 835,
      minDelPrice: 0,
      delPrice: 5,
      averagePrice: 15,
      delTime: 10,
      service: ['支持自取'],
      actives: [{
          activeId: 1,
          acticeText: '满20减10；满200减20；满1000减50；满1000减50'
        },
        {
          activeId: 2,
          acticeText: '本店新用户立减1元'
        },
        {
          activeId: 3,
          acticeText: '折扣商品9折起'
        }
      ]
    }, {
      storeId: 2,
      storeName: '百科超市',
      storeImgUrl: 'https://z3.ax1x.com/2021/05/12/gw1oFK.jpg',
      score: 3.0,
      saleMonth: 835,
      minDelPrice: 0,
      delPrice: 5,
      averagePrice: 15,
      delTime: 5,
      service: ['支持自取', '货到付款'],
      actives: [{
          activeId: 1,
          acticeText: '满20减10；满200减20'
        },
        {
          activeId: 2,
          acticeText: '本店新用户立减1元'
        }
      ]
    }, {
      storeId: 3,
      storeName: '肯德基',
      storeImgUrl: 'https://z3.ax1x.com/2021/05/12/gw1he1.jpg',
      score: 3.0,
      saleMonth: 835,
      minDelPrice: 0,
      delPrice: 5,
      averagePrice: 15,
      delTime: 10,
      service: ['支持自取', '货到付款'],
      actives: [{
          activeId: 1,
          acticeText: '满20减10；满200减20'
        },
        {
          activeId: 2,
          acticeText: '本店新用户立减1元'
        }, {
          activeId: 3,
          acticeText: '折扣商品9折起'
        }, {
          activeId: 4,
          acticeText: '折扣商品9折起'
        }
      ]
    }, {
      storeId: 4,
      storeName: '星星理发店',
      storeImgUrl: 'https://z3.ax1x.com/2021/05/12/gw14dx.jpg',
      score: 3.0,
      saleMonth: 835,
      minDelPrice: 0,
      delPrice: 0,
      averagePrice: 30,
      delTime: 10,
      service: ['货到付款'],
      actives: [{
          activeId: 1,
          acticeText: '满20减10；满200减20'
        },
        {
          activeId: 2,
          acticeText: '本店新用户立减1元'
        }
      ]
    }],
    // 筛选排序栏原始数据
    navItem: [
      {
        id: 0,
        name: "全部美食",
        isActive: false
      },
      {
        id: 1,
        name: "附近",
        isActive: false
      },
      {
        id: 2,
        name: "智能排序",
        isActive: false
      },
      {
        id: 3,
        name: "筛选",
        isActive: false
      }
    ],
    // 下拉选项原始数据
    selectItem: [
      {
        id: 0,
        rank: [
          {
            name: "全部美食",
            itemIsActive: true
          },
          {
            name: "小吃快餐",
            itemIsActive: false
          },
          {
            name: "面包甜点",
            itemIsActive: false
          },
          {
            name: "火锅",
            itemIsActive: false
          },
          {
            name: "烧烤烤串",
            itemIsActive: false
          },
          {
            name: "蛋糕奶茶",
            itemIsActive: false
          },
          {
            name: "川湘菜",
            itemIsActive: false
          },
          {
            name: "西餐",
            itemIsActive: false
          },
          {
            name: "生日蛋糕",
            itemIsActive: false
          },
          {
            name: "其他美食",
            itemIsActive: false
          },
        ],
      },
      {
        id: 1,
        rank: [
          {
            name: "附近",
            itemIsActive: true
          },
          {
            name: "500m",
            itemIsActive: false
          },
          {
            name: "1km",
            itemIsActive: false
          },
          {
            name: "3km",
            itemIsActive: false
          }
        ],
      },
      {
        id: 2,
        rank: [
          {
            name: "智能排序",
            itemIsActive: true
          },
          {
            name: "离我最近",
            itemIsActive: false
          },
          {
            name: "好评优先",
            itemIsActive: false
          },
          {
            name: "销量最高",
            itemIsActive: false
          },
        ]
      },
      {
        id: 3,
        rank: [
          {
            name: "攻击类型",
            itemIsActive: true
          },
          {
            name: "魔法攻击",
            itemIsActive: false
          },
          {
            name: "物理攻击",
            itemIsActive: false
          },
        ]
      }
    ],
    // 下拉选项，被选中那组的数据，由下面的js控制赋值
    selectedItem: [],
    // 记录菜单栏第几项被点开，方便对样式的绑定
    listNum: -1,
    // 下拉选项的隐藏和显示，默认隐藏
    showOrHide: false
  },

  onLoad: function (options) {
    app.sendGetRequest('/community/ec/category?category='+options.category, (res) => {
      console.log(res.data);
      this.setData({
        storeList: res.data
      })
      //console.log(this.storeList);
    }, (err) => {
      console.log(err.errMsg)
    })
    
  },

  showInput: function () {
    this.setData({
        inputShowed: true
    });
  },
  hideInput: function () {
    var v_posts_list = this.data.posts_list
    this.setData({
        inputVal: "",
        inputShowed: false,
        visible_posts_list: v_posts_list
    });
  },
  clearInput: function () {
    this.setData({
        inputVal: ""
    });
  },
  inputTyping: function (e) {
      this.setData({
          inputVal: e.detail.value
      });
  },
  // 标题关键字搜索
  search: function (e){
    var that = this;
      that.data.token = app.getToken()
      //console.log(that.data.token)
      //var communityId = that.data.communityId;
      //网络请求获取账单数据
      wx.request({
        url: 'http://localhost:8080/community/ec/listSearch?name='+e.detail.value,
        method:'GET',
        header:{
          'content-type': 'application/json',
          'Authorization':  that.data.token
        },
        success:function(res){
          // console.log("userInfo" + JSON.stringify(res))
          console.log(res.data);
          that.setData({
            storeList: res.data.data
          });
        },
      }) 
    console.log(e.detail.value)

    // 搜索字段
    // var searchtext = this.data.inputVal
    // var posts_list = this.data.posts_list
    // var v_posts_list = []
    // for(var i=0; i<posts_list.length; i++){
    //   var temp = posts_list[i].title.indexOf(searchtext)
    //   if(temp!=-1){
    //     v_posts_list.push(posts_list[i])
    //   }
    // }
    // this.setData({
    //   visible_posts_list: v_posts_list
    // })
  },

  // 点击菜单栏触发的事件函数
  handleClick: function (e) {
    const index = e.currentTarget.dataset.id;
    const { selectItem, navItem } = this.data;
    // 点击事件，开始时一定会执行的，先令所有下拉选项先隐藏
    const promise = new Promise((res) => {
      this.setData({
        showOrHide: false
      })
      res()
    })
    promise.then(() => {
      // 定义一个延迟0.1秒的函数，和函数动画事件对上
      setTimeout(() => {
        // 当点击的是已经点开的菜单项，则隐藏
        if (this.data.listNum === index) {
          navItem[index].isActive = false
          // 重置所有样式
          this.setData({
            navItem,
            listNum: -1
          })
          return;
        } else {
          // 否则就令当前的index = listNum方便下一次判断
          this.setData({
            listNum: index
          })
        }
        // 令当前点击的菜单栏高亮
        navItem.forEach((v, i) => i === index ? v.isActive = true : v.isActive = false)
        this.setData({
          showOrHide: true,
          selectedItem: selectItem[index].rank,
          navItem
        })
      }, 100)
    })
  },
  // 关闭下拉选项的函数
  // 两种情况下会关闭下拉选项
  //  1. 选择下拉选项，选择后被选的下拉选项高亮，关闭下拉选项列表
  //  2. 点击遮罩层关闭 
  closeMask: function (e) {
    let { navItem, listNum, selectedItem } = this.data;
    // 获取点击的下拉选项内容
    const { contant } = e.currentTarget.dataset
    // 被点击的下拉选项高亮
    selectedItem.forEach((v) => v.name === contant ? v.itemIsActive = true : v.itemIsActive = false)
    // 判断点击的是遮罩层还是下拉选项，如果是下拉选项则把选择的内容赋值到导航栏上，并且把v.isActive = false，让菜单栏高亮消失
    // 如果不是，不用赋值，直接把v.isActive = false，让菜单栏高亮消失
    contant ? (navItem.forEach((v, i) => {
      (i === listNum ? (v.name = contant) : (v.name = v.name));
      v.isActive = false
    })) : (navItem.forEach((v) => v.isActive = false))
    this.setData({
      listNum: -1,
      showOrHide: false,
      navItem,
      selectedItem
    })
  }

})